#include "lwip/api.h"
#include "lwip/memp.h"
#include "lwip/udp.h"

const static char indexdata[] =
"<html> \
<head><title>A test page</title></head> \
<body> \
This is a small test page. \
        </body> \
        </html>";
const static char http_html_hdr[] = 
"HTTP/1.0 200 OK\r\n\
Content-type: text/html\r\n\r\n";

static void process_connection(struct netconn *conn)
{
    struct netbuf *inbuf;
    char *rq;
    u16_t len;
    
    inbuf = netconn_recv(conn);
    netbuf_data(inbuf, &rq, &len);
    /* HTTP "GET /\r\n" */
    if(rq[0] == 'G' && rq[1] == 'E' && rq[2] == 'T') {
        netconn_write(conn, http_html_hdr, sizeof(http_html_hdr),
                NETCONN_NOCOPY);
        netconn_write(conn, indexdata, sizeof(indexdata),
                NETCONN_NOCOPY);
        netconn_close(conn);
    }
    memp_free(MEMP_NETBUF, inbuf);
}
void udp_receive_handle(void *arg, struct udp_pcb *upcb, struct pbuf *p,
				 struct ip_addr *addr, u16_t port)
{
	struct ip_addr dstAdd = *addr;
	printf("Get Connection From %u.%u.%u.%u",ip4_addr1(&dstAdd),ip4_addr2(&dstAdd),
										 ip4_addr3(&dstAdd),ip4_addr4(&dstAdd));
	if(p)
	{
		printf("%Receive Data \n %s\n",p->payload);
		port = 10000;
		udp_sendto(upcb,p,&dstAdd,port);
		pbuf_free(p);
	}

}
void socket_task()
{
	struct udp_pcb *handle;
	handle = udp_new();
	udp_bind(handle,IP_ADDR_ANY,10250);
	
	while(1)
	{
		udp_recv(handle,udp_receive_handle,NULL);
	}

}

